# Definition for a binary tree node.
from typing import List


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def getOneElements(self, node, res = []):
        if node:
            self.getOneElements(node.left, res)
            res.append(node.val)
            self.getOneElements(node.right, res)
    def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]:
        ans, res1, res2 = [], [], []
        self.getOneElements(root1, res1)
        self.getOneElements(root2, res2)
        i,j = 0,0
        while i < len(res1) or j < len(res2):
            if i == len(res1):
              ans.append(res2[j])
              j += 1
              continue
          
            if j == len(res2):
              ans.append(res1[i])
              i += 1
              continue
          
            if res1[i] > res2[j]:
                ans.append(res2[j])
                j += 1
            else:
                ans.append(res1[i])
                i += 1

        return ans